package com.qingcloud.adminbackend.controller;

import cn.hutool.core.collection.CollUtil;
import com.qingcloud.adminbackend.base.AjaxResult;
import com.qingcloud.adminbackend.entity.source.DataSourceDatabaseDto;
import com.qingcloud.adminbackend.entity.sysorder.SysUserOrderMgrDTO;
import com.qingcloud.adminbackend.entity.sysorder.SysUserOrderRequest;
import com.qingcloud.adminbackend.entity.user.SysUser;
import com.qingcloud.adminbackend.service.IDatasourceDatabaseService;
import com.qingcloud.adminbackend.service.ISysOrderService;
import com.qingcloud.adminbackend.service.ISysUserService;
import com.qingcloud.base.utils.CookieUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author qym
 * @Description 数据源数据库管理api
 * @Date 下午4:33 2021/3/29
 **/
@RestController
@RequestMapping("/datasourceDatabase")
public class TdDatasourceDatabaseController {

    @Resource
    IDatasourceDatabaseService datasourceDatabaseService;

    @Resource
    private ISysUserService sysUserService;

    @Resource
    private ISysOrderService sysOrderService;

    @ApiOperation(value = "新增数据源数据库", notes = "新增数据源数据库")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody DataSourceDatabaseDto dataSourceDatabase, HttpServletRequest request) {
        String projectId = CookieUtil.getProjectId(request);
        dataSourceDatabase.setProjectId(projectId);
        return datasourceDatabaseService.add(dataSourceDatabase);
    }

    @ApiOperation(value = "删除数据源数据库", notes = "删除数据源数据库")
    @GetMapping("/deleteByDsIds")
    public AjaxResult deleteByDsIds(@RequestParam("dsIds") String dsIds) {
        return datasourceDatabaseService.deleteByDsIds(dsIds);
    }

    @ApiOperation(value = "修改数据源数据库", notes = "修改数据源数据库")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody DataSourceDatabaseDto dataSourceDatabase) {
        return datasourceDatabaseService.updateDataSourceDatabase(dataSourceDatabase);
    }

    @ApiOperation(value = "复制数据源数据库指定数据源id", notes = "复制数据源数据库指定数据源id", response = DataSourceDatabaseDto.class)
    @GetMapping("/copyByDsId")
    public AjaxResult copyByDsId(@RequestParam("dsId") String dsId) {
        return datasourceDatabaseService.copyByDsId(dsId);
    }

    @ApiOperation(value = "查询数据源指定id", notes = "查询数据源指定id", response = DataSourceDatabaseDto.class)
    @GetMapping("/findByDsIds")
    public AjaxResult<List<DataSourceDatabaseDto>> findByDsIds(@RequestParam("dsIds") String dsIds) {
        return datasourceDatabaseService.findByDsIds(dsIds);
    }

    @ApiOperation(value = "模糊查询数据源数据库指定dsName或者projectId", notes = "模糊查询数据源数据库指定dsName或者projectId", response = DataSourceDatabaseDto.class)
    @GetMapping("/findByNameOrProjectId")
    public AjaxResult<List<DataSourceDatabaseDto>> findByNameOrProjectId(@RequestParam(value = "dsName", required = false) String dsName,
                                                                         HttpServletRequest request) {
        boolean isAdmin = false;
        String userId = CookieUtil.getUserId(request);
        SysUser user = sysUserService.getById(userId);
        if (user.getAdminFlag() == 1) {
            isAdmin = true;
        }
        SysUserOrderRequest orderRequest = new SysUserOrderRequest();
        orderRequest.setUserId(userId);
        orderRequest.setBusinessTypes(Collections.singletonList("5"));

        Set<SysUserOrderMgrDTO> orderSet = sysOrderService.getOrdersByBusinessType(orderRequest, isAdmin);
        if (CollUtil.isEmpty(orderSet)) {
            return AjaxResult.ok(Collections.emptyList());
        }
        Set<String> orderIdSet = orderSet.stream().map(SysUserOrderMgrDTO::getId).collect(Collectors.toSet());
        return datasourceDatabaseService.findByNameOrProjectId(dsName, orderIdSet);
    }

}
